package com.eastfair.imaster.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.echo.core.EchoService;
import com.eastfair.imaster.dto.BulletScreenDTO;
import com.eastfair.imaster.dto.BulletScreenPageQuery;
import com.eastfair.imaster.dto.BulletScreenSaveDTO;
import com.eastfair.imaster.dto.BulletScreenUpdateDTO;
import com.eastfair.imaster.entity.BulletScreen;
import com.eastfair.imaster.service.BulletScreenService;
import com.eastfair.imaster.vo.BulletScreenVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 弹幕前端控制器
 *
 * </p>
 *
 * @author zjn
 * @date 2021-08-31
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/bulletScreen")
@Api(value = "BulletScreen", tags = "")
public class BulletScreenController extends SuperController<BulletScreenService, Long, BulletScreen, BulletScreenPageQuery, BulletScreenSaveDTO, BulletScreenUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Autowired
    private BulletScreenService bulletScreenService;

    @Override
    public void handlerResult(IPage<BulletScreen> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<BulletScreen> bulletScreenList = list.stream().map((map) -> {
            BulletScreen bulletScreen = BulletScreen.builder().build();
            //TODO 请在这里完成转换
            return bulletScreen;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(bulletScreenList));
    }

    /**
     * 删除弹幕
     *
     * @param id 弹幕主键
     * @return 是否删除成功
     */
    @ApiOperation(value = "删除弹幕", notes = "删除弹幕")
    @DeleteMapping("/{id}")
    public R<Boolean> deleteById(@PathVariable("id") Long id) {
        return R.success(bulletScreenService.deleteById(id));
    }

    /**
     * 修改弹幕
     *
     * @param bulletScreenDTO 弹幕实体
     * @return 是否修改成功
     */
    @ApiOperation(value = "修改弹幕", notes = "修改弹幕")
    @PostMapping("/updateById")
    public R<Boolean> updateById(@RequestBody @Validated({BulletScreenDTO.VerifyUpdate.class}) BulletScreenDTO bulletScreenDTO) {
        return R.success(bulletScreenService.updateById(bulletScreenDTO));
    }

    /**
     * 通过id获取弹幕实体
     *
     * @param id 主键
     * @return 弹幕实体
     */
    @ApiOperation(value = "获取弹幕实体", notes = "获取弹幕实体")
    @GetMapping("/queryById/{id}")
    public R<BulletScreenVO> queryById(@PathVariable("id") Long id) {
        return R.success(bulletScreenService.queryById(id));
    }

    /**
     * 获取分页弹幕列表
     *
     * @param params 分页条件
     * @return 分页弹幕列表
     */
    @ApiOperation(value = "获取分页弹幕列表", notes = "获取分页弹幕列表")
    @PostMapping("/queryPageList")
    public R<Page<BulletScreenVO>> queryPageList(@RequestBody PageParams<BulletScreenPageQuery> params) {
        return R.success(bulletScreenService.queryPageList(params));
    }

    /**
     * 根据身份获取弹幕列表
     *
     * @param bulletScreenDTO 弹幕实体过滤条件
     * @return 弹幕列表
     */
    @ApiOperation(value = "根据身份获取弹幕列表", notes = "根据身份获取弹幕列表")
    @PostMapping("/queryList")
    public R<List<BulletScreenVO>> queryList(@RequestBody BulletScreenDTO bulletScreenDTO) {
        return R.success(bulletScreenService.queryList(bulletScreenDTO));
    }

}
